package com.th.supcom.pds.common.client.commom;

import java.util.Date;

import com.extjs.gxt.ui.client.util.DateWrapper;
import com.google.gwt.i18n.client.DateTimeFormat;

public final class PdsClientDateUtils
{
    /** 日期格式化字符串(yyyyMMddHHmm). */
    public static final String DATE_FORMAT_CONTINUOUS_YYYY_MM_DD_HH_MM = "yyyyMMddHHmm";

    /** 日期格式化字符串(yyyyMMddHHmmss). */
    public static final String DATE_FORMAT_CONTINUOUS_YYYY_MM_DD_HH_MM_SS = "yyyyMMddHHmmss";

    /** 日期格式化字符串(HH:mm). */
    public static final String DATE_FORMAT_VERY_SHORT_HH_MM = "HH:mm";

    /** 日期格式化字符串(yyyy-MM-dd). */
    public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";

    /** 日期格式化字符串(yyyy-MM-dd HH:mm). */
    public static final String DATE_FORMAT_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";

    /** 日期格式化字符串(yyyy-MM-dd HH:mm:ss). */
    public static final String DATE_FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    /** 日期格式化字符串(yyyy年MM月). */
    public static final String DATE_FORMAT_YYYY_MM_ZH = "yyyy年MM月";

    private static DateTimeFormat s_dateFormat = DateTimeFormat.getFormat (PdsClientDateUtils.DATE_FORMAT_YYYY_MM_DD);
    private static DateTimeFormat s_dateTimeFormat = DateTimeFormat.getFormat (PdsClientDateUtils.DATE_FORMAT_YYYY_MM_DD_HH_MM_SS);

    private PdsClientDateUtils ()
    {

    }

    /**
     * 判断日期是星期几
     * 
     * @return
     */
    public static int dayForWeek (final Date date)
    {
        final DateWrapper dateWrapper = new DateWrapper (date);
        return dateWrapper.getDayInWeek ();

        /*
         * String dateTime = getYMDHmFormat(date); int year = Integer.parseInt
         * (dateTime.substring (0,4)); int month =
         * Integer.parseInt(dateTime.substring (5,7)); int day =
         * Integer.parseInt(dateTime.substring (8,10));
         * 
         * if (month >= 1 && month <= 2) { month += 12; year -= 1; }
         * 
         * int weekday = (day + 2 * month + 3 * (month + 1) / 5 + year + year /
         * 4 - year / 100 + year / 400 + 1) % 7; return weekday;
         */
    }

    public static Date fillMaxTimeOfDay (final Date date)
    {
        if (null == date)
        {
            return null;
        }
        DateWrapper dw = new DateWrapper (date);
        dw = dw.clearTime ();
        dw = dw.addHours (23);
        dw = dw.addMinutes (59);
        dw = dw.addSeconds (59);
        dw = dw.addMillis (999);
        return dw.asDate ();
    }

    public static Date fillMinTimeOfDay (final Date date)
    {
        if (null == date)
        {
            return null;
        }

        DateWrapper dw = new DateWrapper (date);
        dw = dw.clearTime ();
        dw = dw.addHours (00);
        dw = dw.addMinutes (00);
        dw = dw.addSeconds (00);
        dw = dw.addMillis (000);
        return dw.asDate ();
    }

    @SuppressWarnings ("deprecation")
    public static Date getDateWithDayAdd (final Date date, final int dayAdd)
    {
        if (null == date)
        {
            return null;
        }

        final Date resultDate = (Date) date.clone ();
        resultDate.setDate (resultDate.getDate () + dayAdd);
        return resultDate;
    }

    @SuppressWarnings ("deprecation")
    public static Date getDateWithDayMinus (final Date date, final int dayMinus)
    {
        if (null == date)
        {
            return null;
        }

        final Date resultDate = (Date) date.clone ();
        resultDate.setDate (resultDate.getDate () - dayMinus);
        return resultDate;
    }

    @SuppressWarnings ("deprecation")
    public static Date getDateWithMonthAdd (final Date date, final int monthAdd)
    {
        if (null == date)
        {
            return null;
        }

        final Date resultDate = (Date) date.clone ();
        resultDate.setMonth (resultDate.getMonth () + monthAdd);
        return resultDate;
    }

    /**
     * 比较两个日期相隔的天数 (只要)
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static int getIntervalDay (final Date date1, final Date date2)
    {
        try
        {
            final DateTimeFormat dtf = DateTimeFormat.getFormat (PdsClientDateUtils.DATE_FORMAT_YYYY_MM_DD);
            final Date date1Copy = dtf.parse (dtf.format ((Date) date1.clone ()));
            final Date date2Copy = dtf.parse (dtf.format ((Date) date2.clone ()));

            final long day = (date2Copy.getTime () - date1Copy.getTime ()) / (1000 * 60 * 60 * 24);
            return (int) day;
        }
        catch (final Exception e)
        {
            System.err.println (e);
        }
        return -1;
    }

    @SuppressWarnings ("deprecation")
    public static Date getMaxDateWithMonth (final Date date)
    {
        final int year = date.getYear ();
        final int month = date.getMonth ();
        final Date newDate = new Date (year, month, PdsClientDateUtils.getMaxDay (year, month));
        return newDate;
    }

    private static int getMaxDay (final int year, final int month)
    {
        int day = 0;
        switch (month)
        {
            case 0:
            case 2:
            case 4:
            case 6:
            case 7:
            case 9:
            case 11:
                day = 31;
                break;
            case 1:
                if (PdsClientDateUtils.isLeapYear (year))
                {
                    day = 29;
                }
                else
                {
                    day = 28;
                }
                break;
            case 3:
            case 5:
            case 8:
            case 10:
                day = 30;
                break;
            default:
                break;
        }

        return day;
    }

    @SuppressWarnings ("deprecation")
    public static Date getMinDateWithMonth (final Date date)
    {
        final Date newDate = new Date (date.getYear (), date.getMonth (), 1);
        return newDate;
    }

    @SuppressWarnings ("deprecation")
    public static Date getTodayYMD (final Date date)
    {
        final Date newDate = new Date (date.getYear (), date.getMonth (), date.getDay ());
        return newDate;
    }

    @SuppressWarnings ("deprecation")
    public static Date getTodayYMDHMS (final Date date)
    {
        final Date newDate = new Date (date.getYear (), date.getMonth (), date.getDay (), date.getHours (), date.getMinutes ());
        return newDate;
    }

    public static String getYMDFormat (final Date date)
    {
        if (null == date)
        {
            return "";
        }

        return PdsClientDateUtils.s_dateFormat.format (date);
    }

    public static String getYMDHmsFormat (final Date date)
    {
        if (null == date)
        {
            return "";
        }

        return PdsClientDateUtils.s_dateTimeFormat.format (date);
    }

    public static boolean isLeapYear (final int year)
    {
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }

}
